package com.hmyuan.controller.back;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageInfo;
import com.hmyuan.domain.Files;
import com.hmyuan.service.FilesService;
import com.hmyuan.util.DictionaryUtil;
import com.hmyuan.util.PageUtil;
import com.hmyuan.util.UserUtil;
import com.hmyuan.util.images.ImgForText;

/**
 * 文件controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/files")
public class FilesController {

	@Resource
	FilesService filesService;
	@Resource
	PageUtil pageUtil;
	@Resource
	DictionaryUtil dictionaryUtil;
	@Value("#{configProperties['yuantuPathRoot']}")
	String yuantuPathRoot;
	@Value("#{configProperties['waterOfImg']}")
	String waterOfImg;
	/**
	 * 文件管理页面
	 */
	@RequestMapping("/filesPage")
	public String filesPage(HttpServletRequest request,Model model){
		return "/back/files/filesPage";
	}
	/**
	 * 分页查询文件列表
	 */
	@RequestMapping("/findfilesList")
	public String findfilesList(HttpServletRequest request,Model model,Integer pageNum,Files files){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", filesService.selectListByPage(files, pageNum, 10));
		return "/back/files/filesPageList";
	}
	/**
	 * 文件新增页面
	 */
	@RequestMapping("/filesAddPage")
	public String filesAddPage(HttpServletRequest request,Model model){
		return "/back/files/filesAddPage";
	}
	/**
	 * 新增文件页面提交
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/addfiles")
	public String addfiles(HttpServletRequest request,Model model,Files files){
		filesService.insert(files);
		return "success";
	}
	/**
	 * 新增文件页面上传提交
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/addfilesupload")
	public Map<String, Object> addfilesupload(HttpServletRequest request,Model model,@RequestParam("file") CommonsMultipartFile file) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		if(file==null){
        	result.put("code", 1);
        	result.put("msg", "上传失败");
			return result;
		}
		//上传文件并返回路径
		String filePath = filesService.fileUpload(file);
		//Files files = filesService.insertUpload(file);
		Map<String, String> datamap = new HashMap<String, String>();
		datamap.put("src", pageUtil.getValueOfAplication("fileDomainName")+"/"+filePath);
		
		result.put("code", 0);
		result.put("msg", "上传成功");
		datamap.put("title",filePath);
		result.put("data", datamap);
		return result;
	}
	/**
	 * ---editor.md上传-------------------
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/editormdimagefile")
	public Map<String, Object> editormdimagefile(HttpServletRequest request,Model model,
			@RequestParam("editormd-image-file") CommonsMultipartFile file
			) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		if(file==null){
        	//---editor.md上传-------------------
        	result.put("success", 0);
        	result.put("message", "上传失败");
			return result;
		}
		
		//上传文件并返回路径
		String filePath = filesService.fileUpload(file);
		
		if(filePath==null){
			result.put("success", 0);
        	result.put("message", "上传失败");
		}else{
			result.put("success", 1);
			result.put("message", "上传成功");
			result.put("url", pageUtil.getValueOfAplication("fileDomainName")+filePath);
		}
		return result;
	}
	/**
	 * 根据id删除文件
	 */
	@ResponseBody
	@RequestMapping("/deletefilesById")
	public String deletefilesById(HttpServletRequest request,Model model,Long id){
		if(null==id) return "fail";
		filesService.deletefilesById(id);
		return "success";
	}
}
